# 组合
snippet combine "combine" b
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
 
template <typename T>
void combine_inner(T &data, int start, int n, int m, 
    int depth, T temp, std::vector<T> &result)
{
    if (depth == m - 1)
    {
        //最内层循环 将temp加入result
        for (int i = start; i < n - (m - depth - 1); ++i)
        {
            temp[depth] = data[i];
            result.emplace_back(temp);
        }
    }
    else
        for (int i = start; i < n - (m - depth - 1); ++i)
        {
            temp[depth] = data[i];//每层输出一个元素
            combine_inner(data, i + 1, n, m, depth + 1, temp, result);
        }
}
 
//T可以调入vector<int>, string等，需要支持下标[]操作及size()函数
template <typename T>
std::vector<T> combine(T& data, int m)
{
    if (m <= 0) return{};
    int depth = 0;
    std::vector<T> result;
    T temp(m, 0);
    combine_inner(data, 0, data.size(), m, depth, temp, result);
    return std::move(result);
}
 
template <typename T>
std::vector<T> permutation(T& data)
{
    std::vector<T> results;
    results.emplace_back(data);
    while (std::next_permutation(data.begin(), data.end()))
    {
        results.emplace_back(data.begin(), data.end());
    }
    return std::move(results);
}
 
int main()
{
    string str("abcdef");
    auto combine_rets=combine(str, 3);
    printf("rets.size=%d\n", combine_rets.size());
 
    auto permutation_ret = permutation(str);
    printf("permutation size=%d\n", permutation_ret.size());
    return 0;

endsnippet
